﻿using UnityEngine;

public class GuideCtrl : CSIMediator
{
    private float delayTime;
    private uint timeId;
    private GuidePanel view;

    //新手引导显示对象
    private GameObject mainGo;
    //新手指引目标对象
    private GameObject btnGo;
    //目标对象的父级对象
    private Transform panentTF;

    public override void SetData(params object[] args)
    {
        delayTime = (float)args[0];
    }

    public override void Awake()
    {
        base.Awake();
        view = new GuidePanel(Go);
    }

    public override void Open()
    {
        base.Open();
        ZEventCenter.AddEventListener(ConstData.GUIDE_BREAK, OnBreakGuide);
        view.Info_txt.overflowMethod = UILabel.Overflow.ResizeFreely;

        int width = view.Info_txt.width;
        if (view.Info_txt.width > width)
        {
            view.Info_txt.overflowMethod = UILabel.Overflow.ResizeHeight;
            view.Info_txt.width = width;
        }

        view.Bg_sp.width = view.Info_txt.width + 54;
        view.Bg_sp.height = view.Info_txt.height + 74;

        if (delayTime > 0)  // 说明有延时结束需求
        {
            timeId = ZMgr.TimerMgr.SetTimeOut(ClickGuide, delayTime);
        }
    }

    public override void Close()
    {
        ZEventCenter.RemoveEventListener(ConstData.GUIDE_BREAK, OnBreakGuide);
        base.Close(); 
    }

    public override void Destroy()
    {
        view = null;
        mainGo = null;
        view = null;
        panentTF = null;
        base.Destroy();
    }

    public override void AddListener()
    {
        UIEventListener.Get(view.FullMask_sp.gameObject).onClick = OnClickFullMask;
    }

    public override int PanelId
    {
        get { return (int)PanelID.GuidePanel; }
    }

    private void OnClickFullMask(GameObject go)
    {
        EDebug.Log(go.name);
    }

    private void RefreshView()
    {
        //添加点击事件
        UIEventListener.Get(btnGo).onClick += delegate (GameObject go)
        {
            ClickGuide();
        };
        //返回主界面类型的  先处理按钮本身逻辑 再处理引导逻辑 

        //缓存原有父级
        panentTF = btnGo.transform.parent;
        view.Parent_wgt.transform.position = btnGo.transform.position;
        //把目标物体转到新手指引界面下
        btnGo.transform.parent = view.Parent_wgt.transform;
        //强制把按钮刷到新手界面   待优化
        if (mainGo != null)
        {
            mainGo.transform.parent = btnGo.transform;
            mainGo.transform.localScale = Vector3.one;
            Utility.SetActive(mainGo, true);

            Vector3 mainPos;
            TweenPosition tpos = mainGo.GetComponent<TweenPosition>();
            if (tpos != null)
            {
                mainPos = tpos.from;
                tpos.from = mainPos;
                mainPos = tpos.to;
                tpos.to = mainPos;
            }
            //得到中心位置 
            mainPos = btnGo.GetComponent<UIWidget>().localCenter;
            mainGo.transform.localPosition = mainPos;
            mainGo.transform.localRotation = Quaternion.Euler(0, 0, 0);
        }

        Color color = view.FullMask_sp.color;
        view.FullMask_sp.color = color;
    }

    private void Finish()
    {
        // 说明有延时,把延时取消掉
        if (delayTime > 0)
        {
            ZMgr.TimerMgr.ClearTimeOut(timeId);
        }
        btnGo.transform.parent = panentTF;
        if (mainGo != null)
        {
            mainGo.transform.parent = Go.transform;
            Utility.SetActive(mainGo, false);
        }
        if (mainGo != null)
        {
            Object.Destroy(mainGo);
            mainGo = null;
        }
        ZMgr.FaceCenter.CloseGuideDialog();
        ZMgr.GuideMgr.Finish();
    }

    private void OnBreakGuide(ZEvent ev)
    {
        btnGo.transform.parent = panentTF;
        if (mainGo != null)
        {
            mainGo.transform.parent = Go.transform;
            Utility.SetActive(mainGo, false);
        }
        ZMgr.FaceCenter.CloseGuideDialog();
    }

    public void CancleCurGuide()
    {
        // 说明有延时,把延时取消掉
        if (delayTime > 0)
        {
            ZMgr.TimerMgr.ClearTimeOut(timeId);
        }
        if (mainGo != null)
        {
            mainGo.transform.parent = Go.transform;
            Utility.SetActive(mainGo, false);
        }
        if (mainGo != null)
        {
            Object.Destroy(mainGo);
            mainGo = null;
        }
        ZMgr.FaceCenter.CloseGuideDialog();
    }

    private void ClickGuide()
    {
        UIEventListener.Get(btnGo).onClick -= delegate (GameObject go)
        {
            ClickGuide();
        };
        Finish();
    }
}